package conf

import (
	"context"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
	"wire/internal/controller"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type HttpServer struct {
	Logger *zap.Logger
	Engine *gin.Engine

	UserCtl *controller.UserController
}

func NewGin(logger *zap.Logger) *gin.Engine {
	engine := gin.New()

	// 加载中间件
	// httpMiddle.LoadMiddleware(logger,engine)
	return engine
}

type HttpServerConfig struct {
	Port string `json:"port"`
}

func (receiver *HttpServer) RunServer() {
	var config HttpServerConfig

	config.Port = "9091"
	receiver.ginServer(config)
}

func (receiver *HttpServer) ginServer(config HttpServerConfig) {
	httpServer := &http.Server{
		Addr:         ":" + config.Port,
		Handler:      receiver.Engine,
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 5 * time.Second,
	}

	// 协程启动服务器
	go func() {
		err := httpServer.ListenAndServe()
		if err != nil {
			receiver.Logger.Fatal("服务结束: ", zap.Error(err))
		}
	}()

	// 平滑重启
	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
	<-quit
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()
	if err := httpServer.Shutdown(ctx); err != nil {
		receiver.Logger.Error("关闭服务错误", zap.Error(err))
	}

	receiver.Logger.Info("释放资源中....")
	<-ctx.Done()
	receiver.Logger.Info("退出了....")

}
